/*
 * BookPurchase.fx
 * Author: Yu Min-ting
 * Created on 2009-7-19, 12:20:11
 */

package com.sjtu.fx.user;

import com.sjtu.fx.tools.*;

import com.sjtu.fx.tools.SwingTable.*;

import javafx.scene.CustomNode;

import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.layout.VBox;

import javafx.scene.control.Hyperlink;

import javafx.scene.text.Font;
import javafx.scene.control.Button;

import javafx.io.http.HttpRequest;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.System;

public class purchaseState{
    public var bookName:String;
    public var userName:String;
    public var requestDate:String;
    public var state:String;
}


public class BookPurchase  extends CustomNode {
    var panel:Panel;
    var requests:purchaseState[] = bind requests with inverse;
    public var selection:Integer;
    var purchaseRequest:PurchaseRequest;

    var stateTable:SwingTable = SwingTable{
        columns: [
                    TableColumn {
                        text: "Book Name"
                    },
                    TableColumn {
                        text: "User Name"
                    },
                    TableColumn {
                        text: "Request Date"
                    },
                    TableColumn {
                        text: "State"
                    }
                ]
        rows: bind for (u in requests)
                    TableRow{
                        cells:[
                            TableCell {
                                text:bind u.bookName
                            },
                            TableCell {
                                text:bind u.userName
                            },
                            TableCell {
                                text: bind u.requestDate
                            },
                            TableCell {
                                text: bind u.state
                            },
                        ]
                    }
        selection: bind selection with inverse

    }

    var details = Hyperlink {
            text: "See Details>>"
            font: Font {size:14 name: "Verdana Bold"}
            action: function() {

            }
        }
    var newPurchase = Hyperlink {
            text: "Request New Books>>"
            font: Font {size:14 name: "Verdana Bold"}
            action: function() {
                purchaseRequest.visible = true;
            }
        }

    var refresh = Button {
            translateX:330 translateY:310
            text: "Refresh"
            action: function() {
                var userId:Integer = (this.parent.lookup("loginPanel") as Login).userid;
                System.out.println(userId);
                getUserRequestList(userId);
            }
        }



     override protected function create() : Node {
        getUserRequestList((this.parent.lookup("loginPanel") as Login).userid);
        Group{
            content:[
                 panel = Panel {
                    width:    430
                    height:   550
                    title:    "Purchase Book State"
                    translateX: 0
                    translateY: 0
                },
                VBox{
                    spacing: 25
                    translateX:32 translateY: 60
                    content:[
                        stateTable,
                        newPurchase ,
                        details
                    ]
                },refresh,
                purchaseRequest = PurchaseRequest{
                    visible:false
                }
            ]
        }
     }

     function getUserRequestList(userId:Integer){
         HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/UserRequestList"
       onOutput: function(os:OutputStream) {
            try {
                var json:JSONObject = JSONObject{};
                json.put("userId",userId);
                var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
        try {
                def data:DataInputStream = new DataInputStream(is);
                var json:JSONObject = JSONObject{};
                var result:String = data.readLine();
       //         System.out.println(result);
                json = new JSONObject(result);
                var bookNameList:JSONArray = json.getJSONArray("bookNameList");
                var dateList:JSONArray = json.getJSONArray("dateList");
                var statusList:JSONArray = json.getJSONArray("statusList");
                var i:Integer = 0;
                while(i<bookNameList.length()){
                    requests[i] = purchaseState{
                        bookName:bookNameList.get(i).toString()
                        requestDate:dateList.get(i).toString()
                        state:statusList.get(i).toString()
                        };
                    i++;
                }
            } finally {
                is.close();
            }
        }
    }.start();
     }

}
